package com.lebang.component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.ocean.rawsdk.ApiExecutor;
import com.alibaba.ocean.rawsdk.client.exception.OceanException;
import com.lebang.model.ChannelInfoModel;
import com.umeng.uapp.param.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class UMengCensusComponent {

    /**
     * 友盟apikey
     */
    public static String UMENG_API_KEY = "4053600";
    /**
     * 友盟securitykey
     */
    public static String UMENG_SECURITY_KEY = "g8aEoJrop37S";

    /**
     * 友盟网关地址
     */
    public static String UMENG_SERVER_HOST = "gateway.open.umeng.com";

    @Value("${info.umeng.app-key}")
    private String appKey;




    /**
     * 按照日期,渠道获取友盟活跃用户数
     * @param appStore
     * @param star xxxx-xx-xx
     * @param end xxxx-xx-xx
     * @return
     */
    public Integer queryActiveMemberNum(String appStore,String star,String end,String periodType){
        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor(UMENG_API_KEY, UMENG_SECURITY_KEY);
        apiExecutor.setServerHost(UMENG_SERVER_HOST);
        UmengUappGetActiveUsersByChannelOrVersionParam param = new UmengUappGetActiveUsersByChannelOrVersionParam();
        // 测试环境只支持http
        // param.getOceanRequestPolicy().setUseHttps(false);
        param.setAppkey(appKey);
        param.setStartDate(star);
        param.setEndDate(end);
        param.setPeriodType(periodType);
        param.setChannels(appStore);
        param.setVersions("");

        try {
            UmengUappGetActiveUsersByChannelOrVersionResult result = apiExecutor.execute(param);
            String data = JSONObject.toJSONString(result);
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray newUserInfo = jsonObject.getJSONArray("activeUserInfo");
            JSONObject obj = newUserInfo.getJSONObject(0);
            JSONArray dailyValue = obj.getJSONArray("dailyValue");
            JSONObject active = dailyValue.getJSONObject(0);
            Integer value = active.getInteger("value");
            return value;
        } catch (OceanException e) {
            System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
            return 0;
        }

    }


    /**
     * 按照日期,渠道获取友盟新增用户数
     * @param appStore
     * @param star xxxx-xx-xx
     * @param end xxxx-xx-xx
     * @return
     */
    public Integer queryIncreaseMemberNum(String appStore,String star,String end,String periodType){
        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor(UMENG_API_KEY, UMENG_SECURITY_KEY);
        apiExecutor.setServerHost(UMENG_SERVER_HOST);

        UmengUappGetNewUsersByChannelOrVersionParam param = new UmengUappGetNewUsersByChannelOrVersionParam();
        // 测试环境只支持http
        // param.getOceanRequestPolicy().setUseHttps(false);
        param.setAppkey(appKey);
        param.setStartDate(star);
        param.setEndDate(end);
        param.setPeriodType(periodType);
        param.setChannels(appStore);
        param.setVersions("");

        try {
            UmengUappGetNewUsersByChannelOrVersionResult result = apiExecutor.execute(param);
            String data = JSONObject.toJSONString(result);
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray newUserInfo = jsonObject.getJSONArray("newUserInfo");
            JSONObject obj = newUserInfo.getJSONObject(0);
            JSONArray dailyValue = obj.getJSONArray("dailyValue");
            JSONObject active = dailyValue.getJSONObject(0);
            Integer value = active.getInteger("value");
            return value;
        } catch (OceanException e) {
            System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
            return 0;
        }

    }


    /**
     * 获取所有数据源的数据
     * @param queryTime
     * @param page
     * @param limit
     * @return
     */
    public List<ChannelInfoModel> queryAllAppStore(String queryTime, int page, int limit) {


        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor(UMENG_API_KEY, UMENG_SECURITY_KEY);
        apiExecutor.setServerHost(UMENG_SERVER_HOST);

        UmengUappGetChannelDataParam param = new UmengUappGetChannelDataParam();
        // 测试环境只支持http
        // param.getOceanRequestPolicy().setUseHttps(false);
        param.setAppkey(appKey);
        param.setDate(queryTime);
        param.setPerPage(limit);
        param.setPage(page);

        try {
            UmengUappGetChannelDataResult result = apiExecutor.execute(param);
            String data = JSONObject.toJSONString(result);
            JSONObject jsonObject = JSONObject.parseObject(data);
            String newUserInfo = jsonObject.getString("channelInfos");
            List<ChannelInfoModel> channelInfoModels = JSONObject.parseArray(newUserInfo, ChannelInfoModel.class);
            return channelInfoModels;

        } catch (OceanException e) {
            System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
            return null;
        }

    }



    /**
     * 按照日期获取友盟活跃用户数
     * @param star xxxx-xx-xx
     * @param end xxxx-xx-xx
     * @return
     */
    public Integer queryActiveMember(String star,String end,String periodType){
        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor(UMENG_API_KEY, UMENG_SECURITY_KEY);
        apiExecutor.setServerHost(UMENG_SERVER_HOST);
        UmengUappGetActiveUsersParam param = new UmengUappGetActiveUsersParam();
        // 测试环境只支持http
        // param.getOceanRequestPolicy().setUseHttps(false);
        param.setAppkey(appKey);
        param.setStartDate(star);
        param.setEndDate(end);
        param.setPeriodType(periodType);

        try {
            UmengUappGetActiveUsersResult result = apiExecutor.execute(param);
            String data = JSONObject.toJSONString(result);
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray newUserInfo = jsonObject.getJSONArray("activeUserInfo");
            JSONObject obj = newUserInfo.getJSONObject(0);
            Integer value = obj.getInteger("value");
            return value;
        } catch (OceanException e) {
            System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
            return 0;
        }

    }


    /**
     * 按照日期获取友盟新增用户数
     * @param star xxxx-xx-xx
     * @param end xxxx-xx-xx
     * @return
     */
    public Integer queryIncreaseMember(String star,String end,String periodType){
        // 请替换apiKey和apiSecurity
        ApiExecutor apiExecutor = new ApiExecutor(UMENG_API_KEY, UMENG_SECURITY_KEY);
        apiExecutor.setServerHost(UMENG_SERVER_HOST);

        UmengUappGetNewUsersParam param = new UmengUappGetNewUsersParam();
        // 测试环境只支持http
        // param.getOceanRequestPolicy().setUseHttps(false);
        param.setAppkey(appKey);
        param.setStartDate(star);
        param.setEndDate(end);
        param.setPeriodType(periodType);

        try {
            UmengUappGetNewUsersResult result = apiExecutor.execute(param);
            String data = JSONObject.toJSONString(result);
            JSONObject jsonObject = JSONObject.parseObject(data);
            JSONArray newUserInfo = jsonObject.getJSONArray("newUserInfo");
            JSONObject obj = newUserInfo.getJSONObject(0);
            Integer value = obj.getInteger("value");
            return value;
        } catch (OceanException e) {
            System.out.println("errorCode=" + e.getErrorCode() + ", errorMessage=" + e.getErrorMessage());
            return 0;
        }

    }



}
